// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    todo: '', //输入的值
    todos: [], //todo列表
    leftCount: 1, //未完成的项数
    allFinished: false, //全部完成
    allSetting: true, // 全部未完成
    clearSetting: true //清空所有已完成
  },
  // 保存todos到本地
  save() {
    wx.setStorageSync('todos', this.data.todos)
  },
  // 渲染出todo
  onShow() {
    var todos = wx.getStorageSync('todos');
    if (todos) {
      var leftCount = todos.filter(function (item) {
        return !item.finished;
      }).length;
      this.setData({
        todos: todos,
        leftCount: leftCount,
        allFinished: !leftCount
      })

    }

    var allSetting = wx.getStorageSync('allSetting');
    if (typeof allSetting == 'boolean') {
      this.setData({
        allSetting: allSetting
      });
    }

    var clearSetting = wx.getStorageSync('clearSetting');
    if (typeof clearSetting == 'boolean') {
      this.setData({
        clearSetting: clearSetting
      });
    }
  },
  // 监听todo输入
  inputTodo(e) {
    this.setData({
      todo: e.detail.value
    })
  },
  // 添加todo
  addTodo() {
    // 先判断有没有值
    if (!this.data.todo || !this.data.todo.trim()) {
      return
    }
    var todos = this.data.todos;
    var todo = {
      content: this.data.todo,
      finished: false,
      id: +new Date()
    }
    todos.push(todo);
    this.setData({
      todo: '',
      todos: todos,
      leftCount: this.data.leftCount + 1
    })
    this.save();
    getApp().writeHistory(todo, 'create', +new Date());
  },
  // 删除todo
  onItemRemove: function (e) {
    var index = e.currentTarget.dataset.index;
    var todos = this.data.todos;
    var remove = todos.splice(index, 1)[0];
    this.setData({
      todos: todos,
      leftCount: this.data.leftCount - (remove.finished ? 0 : 1)
    });
    this.save();
    getApp().writeHistory(remove, 'delete', +new Date());
  },
  // 切换todo Item
  toggleTodo: function (e) {
    var index = e.currentTarget.dataset.index;
    var todos = this.data.todos;
    var todo = todos[index];
    todo.finished = !todo.finished;
    var leftCount = this.data.leftCount + (todo.finished ? -1 : 1);
    this.setData({
      todos: todos,
      leftCount: leftCount,
      allFinished: !leftCount
    });
    this.save();
    getApp().writeHistory(todo, todo.finished ? 'finish' : 'restart', +new Date());
  },

  // 切换所有todo状态
  toggleAll: function (e) {
    var allFinished = !this.data.allFinished;
    var todos = this.data.todos.map(function (todo) {
      todo.finished = allFinished;
      return todo;
    });
    this.setData({
      todos: todos,
      leftCount: allFinished ? 0 : todos.length,
      allFinished: allFinished
    })
    this.save();
    getApp().writeHistory(null, allFinished ? 'finishAll' : 'restartAll', +new Date());
  },
  // 清除所有已完成
  clearFinished: function (e) {
    var todos = this.data.todos;
    var remains = todos.filter(function (todo) {
      return !todo.finished;
    });
    this.setData({
      todos: remains
    });
    this.save();
    getApp().writeHistory(null, 'clear', +new Date());
  },

  // 跳转到添加待办详情页
  createItem() {
    wx.navigateTo({
      url: '/pages/detail/detail',
    })
  }
})